﻿/*
LICENCE___________________________________________________________________
    Copyright (C) 2012 Reverdy Sylvain

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
FIN LICENCE______________________________________________________________
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Merise_designer.Donnees
{
    class CL_DictionnaireDeDonnees
    {
        private CL_CAD cad = new CL_CAD();

        private int nextID=0;

        public void addSimpleEntite(Objets.CL_DictionnaireDeDonnees dico)
        {
            string req = @"INSERT INTO DICTIONNAIRE (ID_DIC,NOM_DIC, CODE_DIC, TYPE_DIC, ID_ENTITE,PK,REQ) VALUES ('" + dico.id + "','" + dico.nom + "','" + dico.code + "','" + dico.idType + "','" + dico.idEntite + "','" + convertBoolToAccess(dico.pk) + "','" + convertBoolToAccess(dico.O) + "') ";
            cad.ActionsRows(req, true);
        }
        public void addSimpleAssociation(Objets.CL_DictionnaireDeDonnees dico)
        {
            string req = @"INSERT INTO DICTIONNAIRE (ID_DIC,NOM_DIC, CODE_DIC, TYPE_DIC, ID_ASSO,PK,REQ) VALUES ('" + dico.id + "','" + dico.nom + "','" + dico.code + "','" + dico.idType + "','" + dico.idAsso + "','" + convertBoolToAccess(dico.pk) + "','" + convertBoolToAccess(dico.O) + "') ";
            cad.ActionsRows(req, true);
        }
        public void add(Objets.CL_DictionnaireDeDonnees dico)
        {
            string req="";
            if (dico.idEntite ==null||dico.idEntite =="" )
            {
                req = @"INSERT INTO DICTIONNAIRE (ID_DIC,NOM_DIC, CODE_DIC, TYPE_DIC, ID_ASSO, PK,REQ) VALUES('" + dico.id.ToString() + "','" + dico.nom + "','" + dico.code + "','" + dico.idType + "','" + dico.idAsso + "', ' " + convertBoolToAccess(dico.pk) + "','" + convertBoolToAccess(dico.O) + "') ";
            }
            else if (dico.idAsso==null || dico.idAsso == "")
            {
                req = @"INSERT INTO DICTIONNAIRE (ID_DIC,NOM_DIC, CODE_DIC, TYPE_DIC, ID_ENTITE, PK,REQ) VALUES('" + dico.id.ToString() + "','" + dico.nom + "','" + dico.code + "','" + dico.idType + "', '" + dico.idEntite + "',' " + convertBoolToAccess(dico.pk) + "','" + convertBoolToAccess(dico.O) + "') ";
            }
                cad.ActionsRows(req, true);
        }

        public String getAdd(Objets.CL_DictionnaireDeDonnees dico)
        {
            string req = "";
            if (dico.idEntite == null || dico.idEntite == "")
            {
                return @"INSERT INTO DICTIONNAIRE (ID_DIC,NOM_DIC, CODE_DIC, TYPE_DIC, ID_ASSO, PK,REQ) VALUES('" + dico.id.ToString() + "','" + dico.nom + "','" + dico.code + "','" + dico.idType + "','" + dico.idAsso + "', ' " + convertBoolToAccess(dico.pk) + "','" + convertBoolToAccess(dico.O) + "');";
            }
            else if (dico.idAsso == null || dico.idAsso == "")
            {
               return @"INSERT INTO DICTIONNAIRE (ID_DIC,NOM_DIC, CODE_DIC, TYPE_DIC, ID_ENTITE, PK,REQ) VALUES('" + dico.id.ToString() + "','" + dico.nom + "','" + dico.code + "','" + dico.idType + "', '" + dico.idEntite + "',' " + convertBoolToAccess(dico.pk) + "','" + convertBoolToAccess(dico.O) + "');";
            }
            return req;
        }

        public void update(Objets.CL_DictionnaireDeDonnees dico)
        {
            string req = @"UPDATE DICTIONNAIRE SET NOM_DIC='" + dico.nom + "', CODE_DIC='" + dico.code + "', TYPE_DIC='" + dico.idType + "', PK='" + convertBoolToAccess(dico.pk) + "', REQ='" + convertBoolToAccess(dico.O) + "' WHERE ID_DIC=" + dico.id + "";
            cad.ActionsRows(req, true);
        }
        public void deleteAll()
        {
            string req = @"DELETE FROM DICTIONNAIRE;";
            cad.ActionsRows(req, true);
        }
        public String getDeleteAll()
        {
            return @"DELETE FROM DICTIONNAIRE;";
        }

        public void delete(Objets.CL_DictionnaireDeDonnees dico)
        {
            string req = @"DELETE FROM DICTIONNAIRE WHERE ID_DIC=" + dico.id + "";
            cad.ActionsRows(req, true);
        }
        public void deleteAllFromEntite(Objets.CL_Entite entite)
        {
            string req = @"DELETE FROM DICTIONNAIRE WHERE ID_ENTITE = '" + entite.Id + "'";
            cad.ActionsRows(req, true);
        }
        public void deleteAllFromAssociation(Objets.CL_Association asso)
        {
            string req = @"DELETE FROM DICTIONNAIRE WHERE ID_ASSO = '" + asso.Id + "'";
            cad.ActionsRows(req, true);
        }
        public DataSet selectAll(string monJeu)
        {
            string req = @"SELECT ID_DIC, NOM_DIC, CODE_DIC, TYPE_DIC, ID_ASSO, ID_ENTITE, PK, REQ FROM DICTIONNAIRE";
            return cad.GetRows(req, monJeu);
        }
        public DataSet select(Objets.CL_DictionnaireDeDonnees dico, string monJeu)
        {
            string req = @"SELECT ID_DIC, NOM_DIC, CODE_DIC, TYPE_DIC, ID_ASSO, ID_ENTITE, PK, REQ FROM DICTIONNAIRE WHERE ID_DIC='" + dico.id + "'";
            return cad.GetRows(req, monJeu);
        }
        public DataSet selectAllFromEntite(Objets.CL_Entite entite, string monJeu)
        {
            string req = @"SELECT ID_DIC, NOM_DIC, CODE_DIC, TYPE_DIC, TYPES.NOM_TYPE, ID_ASSO, ID_ENTITE, PK, REQ FROM DICTIONNAIRE INNER JOIN TYPES ON (DICTIONNAIRE.TYPE_DIC = TYPES.ID_TYPE) WHERE ID_ENTITE='"+entite.Id+"'";
            return cad.GetRows(req, monJeu);
        }
        public DataSet selectAllFromVisualEntite(Objets.CL_Entite entite, string monJeu)//Pour l'affichage
        {
            string req = @"SELECT NOM_DIC as Nom, TYPES.NOM_TYPE as Type, pk as PK, REQ FROM DICTIONNAIRE INNER JOIN TYPES ON (DICTIONNAIRE.TYPE_DIC = TYPES.ID_TYPE) WHERE ID_ENTITE='" + entite.Id + "'";
            return cad.GetRows(req, monJeu);
        }
        public DataSet selectAllFromAssociation(Objets.CL_Association asso, string monJeu)
        {
            string req = @"SELECT ID_DIC, NOM_DIC, CODE_DIC, TYPE_DIC, TYPES.NOM_TYPE, ID_ASSO, ID_ENTITE, PK, REQ FROM DICTIONNAIRE INNER JOIN TYPES ON (DICTIONNAIRE.TYPE_DIC = TYPES.ID_TYPE) WHERE ID_ASSO='" + asso.Id + "'";
            return cad.GetRows(req, monJeu);
        }
        public DataSet selectAllFromVisualAssociation(Objets.CL_Association asso, string monJeu)//Pour l'affichage
        {
            string req = @"SELECT NOM_DIC as Nom, TYPES.NOM_TYPE as Type, pk as PK, REQ FROM DICTIONNAIRE INNER JOIN TYPES ON (DICTIONNAIRE.TYPE_DIC = TYPES.ID_TYPE) WHERE ID_ASSO='" + asso.Id + "'";
            return cad.GetRows(req, monJeu);
        }

        //Génère un ID auto
        public int getNextIdAuto()
        {
            if (nextID == 0)
            {
                string req = @"SELECT ID_DIC FROM DICTIONNAIRE ORDER BY ID_DIC DESC";
                foreach (System.Data.DataRow theRow in cad.GetRows(req, "monjeu").Tables["monjeu"].Rows)
                {
                    nextID=int.Parse(theRow["ID_DIC"].ToString()) + 1;
                    return nextID;
                }
                nextID=1;
            }
            nextID += 1;
            return nextID;
        }
        public void setIdAuto(int value)
        {
            nextID = value;
        }

        public int convertBoolToAccess(Boolean value)
        {
            if (value)
            {
                return -1;
            }
            return 0;
        }
        public Boolean convertAccessToBool(int value)
        {
            if (value==-1)
            {
                return true;
            }
            return false;
        }
    }
}
